import React from 'react';

export default function index() {
  // 1、哈希-字母异位分组
  const sortStr = (arr) => {
    let result = new Object();
    for (let i of arr) {
      let count = new Array(26).fill(0); // 记录字符串中字母出现的次数
      for (let j of i) {
        count[j.charCodeAt() - 'a'.charCodeAt()]++;
      }
      result[count] ? result[count].push(i) : (result[count] = [i]); // 以字母出现情况数组作为哈希键，值用数组存储（解决哈希冲突），哈希函数就是对象键值的映射
    }
    return Object.values(result);
  };
  console.log(sortStr(['eat', 'tea', 'tan', 'ate', 'nat', 'bat']));

  //数组中最长连续序列
  const maxLengthArr = (arr) => {
    if (arr.length === 0) return 0;
    // 去重
    const newSet = new Set(arr); //Set 是基于哈希表（或类似高效数据结构）实现的，它能在 O(1) 时间复杂度 内完成元素的 add、has、delete 操作。
    let maxlen = 0;
    for (let num of newSet) {
      if (!newSet.has(num - 1)) {
        //找到有序序列起点
        let curentNum = num;
        let len = 1;
        while (newSet.has(curentNum + 1)) {
          len += 1;
          curentNum += 1;
        }
        maxlen = Math.max(len, maxlen);
      }
    }
    return maxlen;
  };
  console.log(maxLengthArr([100, 4, 200, 3, 0, 1, 5]));

  // 双指针
  // 移动零到数组末尾
  const moveZore = (arr) => {
    let FirstZero = 0; // 记录第一个零的位置
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] !== 0 && arr[FirstZero] === 0) {
        arr[FirstZero] = arr[i];
        arr[i] = 0;
        FirstZero += 1;
      } else if (arr[FirstZero] !== 0) {
        FirstZero += 1;
      }
    }
    return arr;
  };
  console.log(moveZore([1, 2, 3, 0, 4, 0, 1, 1]));
  // 盛水最多
  // 1、双指针暴力算法
  const getMoreWater1 = (height) => {
    let maxWater = 0;
    for (let i = 0; i < height.length; i++) {
      for (let j = i + 1; j < height.length; j++) {
        let water = (j - i) * Math.min(height[j], height[i]);
        maxWater = Math.max(water, maxWater);
      }
    }
    return maxWater;
  };
  const getMoreWater2 = (height) => {
    // 两个指针分别指向开头和结尾
    // 体积=Math.min(height[front], height[last]) * (last - front)，所以每次移动height较小的指针
    let front = 0;
    let last = height.length - 1;
    let maxWater = Math.min(height[front], height[last]) * (last - front);
    while (last - front !== 1) {
      if (height[front] > height[last]) {
        last -= 1;
      } else {
        front += 1;
      }
      const currentWater =
        Math.min(height[front], height[last]) * (last - front);
      maxWater = Math.max(currentWater, maxWater);
    }
    return maxWater;
  };
  console.log(getMoreWater2([8, 7, 2, 1]));
  return <div>index</div>;
}
